package cn.controller;

import cn.entity.Message;
import cn.entity.Text;
import cn.entity.User;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;

@RestController
@RequestMapping("test")
public class TestController {

    final static Base64.Encoder encoder = Base64.getEncoder();
    final static Base64.Decoder decoder = Base64.getDecoder();

    public static String encode(String text) {
        return encoder.encodeToString(text.getBytes(StandardCharsets.UTF_8));
    }

    public static String decode(String encodedText) {
        return new String(decoder.decode(encodedText), StandardCharsets.UTF_8);
    }

    @GetMapping("add")
    @ResponseBody
    public String add(Message data){
        System.out.println(data);
        if (data == null){
            return JSONObject.toJSONString(new User("未传入参数"));
        }
        System.out.println(JSONObject.toJSONString(data));
        return JSONObject.toJSONString(data);
    }

    @GetMapping("add/{data}")
    public String addPath(@PathVariable("data") String data){
        System.out.println("路径传参："+data);
        if (data == null){
            return JSONObject.toJSONString(new User("未传入参数"));
        }
        System.out.println(JSONObject.toJSONString(data));
        return JSONObject.toJSONString(data);
    }

    @PostMapping("add")
    public String addPost(@RequestBody Message data){
        System.out.println(data);
        if (data == null){
            return JSONObject.toJSONString(new User("未传入参数"));
        }
        System.out.println(JSONObject.toJSONString(data));
        return JSONObject.toJSONString(data);
    }

    @GetMapping("move")
    public String move() throws InterruptedException {
        Message message = new Message();
        message.setData("api");
        ArrayList<Text> list = new ArrayList<>();
        list.add(new Text("xian"));
        list.add(new Text("guangzhou"));
        message.setText(list);
        return JSONObject.toJSONString(message);
    }

    @GetMapping("encrypt")
    public String encrypt(String data){
        System.out.println("encrypt:==>"+JSONObject.toJSONString(data));
        return data;
    }

    @GetMapping("decrypt")
    public String decrypt(String data){
        System.out.println("decrypt:==>"+encode(data));
        return encode(data);
    }

    public static void main(String[] args) {
        String str = "encrypt";

        System.out.println("编码后的字符串为：");
        System.out.println(encode(str));

        System.out.println("解码后的字符串为：");
        System.out.println(decode(encode(str)));
    }

    @GetMapping("update")
    public String update(){
        System.out.println("update");
        return "text:update";
    }

    @PostMapping("message")
    public void getMessage(@RequestBody Message message){
        System.out.println(message);
    }

    /**
     * {
     *     "title":"天谕",
     *     "processName":"不将就",
     *     "body":"该回归了"
     * }
     */
}
